package com.my.netty.promote.server.handler;

import com.my.netty.promote.message.GroupMembersRequestMessage;
import com.my.netty.promote.message.GroupMembersResponseMessage;
import com.my.netty.promote.server.session.GroupSession;
import com.my.netty.promote.server.session.GroupSessionFactory;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.Set;

@ChannelHandler.Sharable
public class GroupMembersRequestMessageHandler extends SimpleChannelInboundHandler<GroupMembersRequestMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, GroupMembersRequestMessage message) throws Exception {
        String groupName = message.getGroupName();
        //暂不做存在校验
        GroupSession groupSession = GroupSessionFactory.getSession();
        Set<String> members = groupSession.getMembers(groupName);
        GroupMembersResponseMessage responseMessage = new GroupMembersResponseMessage(members);
        ctx.writeAndFlush(responseMessage);
    }
}
